package net.keebook.webapp.bean;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import net.keebook.webapp.dao.KeeBookDao;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.entity.KeeBookAccess;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccessRight;
import net.keebook.webapp.util.Messages;

@RequestScoped
@ManagedBean
public class RenameKeeBookBean {
	private String keeBookName;
	private SessionModel sessionModel;
	
	@Inject
	private KeeBookDao keeBookDao;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		KeeBook keeBook = getKeeBookDao().get(getSessionModel().getKeeBookId());
		setKeeBookName(keeBook.getName());
		
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String exposableId = request.getParameter("delete");
		if (exposableId != null && !"".equals(exposableId)) {
			KeeBookAccess accessRight = getKeeBookDao().getAccessRight(getSessionModel().getUserId(), keeBook.getId());
			
			// If key box to be deleted is current key box, update state
			if (keeBook.getId().equals(getSessionModel().getKeeBookId())) {
				getSessionModel().setKeeBookId(null);
			}
			
			if (accessRight.getAccessRight().equals(AccessRight.OWNER))  {
				// If user if owner of key box, delete entire key box with all items
				getKeeBookDao().delete(keeBook);
				FacesContext.getCurrentInstance().getExternalContext().redirect("selectkeebook.xhtml?msg="+Messages.KEYBOX_DELETED);
				return;
			} else {
				// If user is not owner of key box, just remove him from the list of access rights
				getKeeBookDao().removeAccessRight(getSessionModel().getUserId(), keeBook.getId());
				FacesContext.getCurrentInstance().getExternalContext().redirect("selectkeebook.xhtml?msg="+Messages.KEYBOX_UNASSIGNED);
				return;
			}
		}
	}
	
	public void rename() throws IOException {
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.OWNER)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		KeeBook keeBook = getKeeBookDao().get(getSessionModel().getKeeBookId());
		keeBook.setName(getKeeBookName());
		getKeeBookDao().update(keeBook);
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	}

	public String getKeeBookName() {
		return keeBookName;
	}

	public void setKeeBookName(String keeBookName) {
		this.keeBookName = keeBookName;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}

	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}
}
